<template>
  <div>
    <!-- 条件查询 -->
    <el-form :inline="true" :model="query" size="mini">
      <el-form-item label="文章标题:">
        <el-input v-model.trim="query.title"></el-input>
      </el-form-item>
      <el-form-item label="状态:">
        <el-select v-model="query.status" clearable filterable>
          <el-option label="未审核" :value="1"></el-option>
          <el-option label="审核通过" :value="2"></el-option>
          <el-option label="审核未通过" :value="3"></el-option>
          <el-option label="已删除" :value="0"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" type="primary" @click="queryData"
          >查询</el-button
        >
        <el-button icon="el-icon-refresh" @click="reload">重置</el-button>
        <el-button icon="el-icon-circle-plus-outline" type="primary"
          >新增</el-button
        >
      </el-form-item>
    </el-form>

    <!-- 显示列表   -->
    <el-table :data="list" stripe border style="width: 100%">
      <el-table-column align="center" label="序号" width="60" type="index">
      </el-table-column>
      <el-table-column align="center" prop="title" label="文章标题">
      </el-table-column>
      <el-table-column align="center" prop="viewCount" label="浏览数">
      </el-table-column>
      <el-table-column align="center" prop="thumhup" label="点赞数">
      </el-table-column>
      <el-table-column align="center" prop="ispublic" label="是否公开">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.ispublic == 0" type="warning">公开</el-tag>
          <el-tag v-if="scope.row.ispublic == 1" type="error">不公开</el-tag>
        </template>
      </el-table-column>
      <el-table-column align="center" prop="status" label="状态">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" type="error">已删除</el-tag>
          <el-tag v-if="scope.row.status == 1" type="warning">未审核</el-tag>
          <el-tag v-if="scope.row.status == 2" type="success ">已审核</el-tag>
          <el-tag v-if="scope.row.status == 3" type="info ">审核未通过</el-tag>
        </template>
      </el-table-column>
      <el-table-column
        align="center"
        prop="updateDate"
        label="最后更新时间"
        min-width="90px"
      >
        <template slot-scope="scope">
          {{ getFormat(scope.row.updateDate) }}
        </template>
      </el-table-column>
      <el-table-column align="left" label="操作" width="220px">
        <template slot-scope="scope">
          <!-- 只有在status==1的时候才有审核按钮 -->
          <el-button type="primary" size="mini" @click="openView(scope.row.id)"
            >查看</el-button
          >
          <el-button
            v-if="scope.row.status == 1"
            type="success"
            size="mini"
            @click="openAudit(scope.row.id)"
            >审核</el-button
          >
          <el-button
            v-if="scope.row.status !== 0"
            type="danger"
            size="mini"
            @click="handleDelete(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page.current"
      :page-sizes="[10, 20, 30, 40]"
      :page-size="page.size"
      layout="total, sizes, prev, pager, next, jumper"
      :total="page.total"
    >
    </el-pagination>

    <!-- 审核组件 -->
    <audit
      :title="audit.title"
      :visible="audit.visible"
      :remoteClose="remoteClose"
      :id="audit.id"
      :isAudit="audit.isAudit"
    >
    </audit>
  </div>
</template>

<script>
import api from "@/api/article";
//格式化日期
import { format } from "@/utils/date.js";
import Audit from "./audit";
export default {
  data() {
    return {
      list: [],
      page: {
        current: 1,
        size: 20,
        total: 0,
      }, //分页
      query: {},
      audit: {
        title: "",
        visible: false,
        id: null,
        isAudit: true, //true是审核，false是查看
      },
    };
  },
  created() {
    this.fetchData();
  },
  components: {
    Audit,
  },

  methods: {
    fetchData() {
      api.getList(this.query, this.page.current, this.page.size).then((res) => {
        this.list = res.data.records;
        this.page.total = res.data.total;
      });
    },
    // 格式化日期
    getFormat(date) {
      return format(date);
    },
    handleSizeChange(val) {
      this.page.size = val;
      this.fetchData();
    },
    handleCurrentChange(val) {
      this.page.current = val;
      this.fetchData();
    },
    queryData() {
      // 页码设置为1
      this.page.current = 1;
      this.fetchData();
    },
    reload() {
      this.query = {};
      this.fetchData();
    },
    //打开审核
    openAudit(id) {
      this.audit.id = id;
      this.audit.isAudit = true;
      this.audit.title = "审核文章";
      this.audit.visible = true;
    },
    //查看文章
    openView(id) {
      this.audit.id = id;
      this.audit.isAudit = false;
      this.audit.title = "查看文章";
      this.audit.visible = true;
    },
    //删除文章
    handleDelete(id) {
      this.$confirm("确认删除文章吗?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          //发送删除请求
          api.deleteById(this.id).then((res) => {
            this.$message({
              type: res.code == 20000 ? "success" : "error",
              message: res.message,
            });
            this.fetchData();
          });
        })
        .catch(() => {});
    },
    //关闭窗口
    remoteClose() {
      this.audit.visible = false;
      this.fetchData();
    },
  },
};
</script>

<style scoped>
</style>